#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "jsoncpppath.h"

#include <QtWidgets>

#define AppPath (qApp->applicationDirPath() + "/AppIcon.appiconset")
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QDir().mkpath(AppPath);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_choose_clicked()
{
    auto file = QFileDialog::getOpenFileName(this, "choose image", qApp->applicationDirPath());
    if(file.isEmpty())
        return;
    ui->label->setPixmap(QPixmap(file));
    imagepath = file;
}


void MainWindow::on_pushButton_make_clicked()
{
    exportImage(":/json/Contents_all.json");
    exportImage(":/json/Contents_icon.json");
    QFile::copy(":/json/Contents_all.json", AppPath + "/Contents.json");
    QMessageBox::information(this, " ", "done");
}

void MainWindow::exportImage(const QString &jsonpath)
{
    QImage image(imagepath);
    JsonCppPathReader reader(jsonpath);
    for(auto ptr : reader.getChildrenJsonPtr("/images")) {
        double w = ptr->value("/size").toString().split("x").value(0).toDouble();
        double h = ptr->value("/size").toString().split("x").value(1).toDouble();
        double s =  ptr->value("/scale").toString().mid(0, 1).toDouble();

        image.scaled(w * s, h * s).save(AppPath + "/" + ptr->value("/filename").toString());
    }
}

